package com.hushui.mall.commodity.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hushui.mall.commodity.entity.SpuInfoEntity;
import com.hushui.mall.commodity.service.SpuInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hushui.mall.commodity.entity.SkuInfoEntity;
import com.hushui.mall.commodity.service.SkuInfoService;
import com.hushui.common.utils.PageUtils;
import com.hushui.common.utils.R;



/**
 * sku信息
 *
 * @author miya
 * @email sunlightcs@gmail.com
 * @date 2023-03-16 11:38:45
 */
@Slf4j
@RestController
public class SkuInfoController {
    @Autowired
    private SkuInfoService skuInfoService;
    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 列表
     */@RequestMapping("commodity/skuinfo/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = skuInfoService.queryPage(params);

        return R.ok().put("page", page);
    }
    @GetMapping("/product/spuinfo/skuId/{skuId}")
    public R getSpuInfoBySkuId(@PathVariable(value = "skuId")Long id){
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("sku_id",id);
        SkuInfoEntity one = skuInfoService.getOne(wrapper);
        SpuInfoEntity byId = spuInfoService.getById(one.getSpuId());
        return R.ok().put(("data"),byId);
    }



    /**
     * 信息
     */
    @RequestMapping("commodity/skuinfo/info/{skuId}")
    public R info(@PathVariable("skuId") Long skuId){
		SkuInfoEntity skuInfo = skuInfoService.getById(skuId);
        return R.ok().put("data", skuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("commodity/skuinfo/save")
    public R save(@RequestBody SkuInfoEntity skuInfo){
		skuInfoService.save(skuInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("commodity/skuinfo/update")
    public R update(@RequestBody SkuInfoEntity skuInfo){
		skuInfoService.updateById(skuInfo);

        return R.ok();
    }
    @RequestMapping("/product/skuinfo/list")
    public R getSku(@RequestParam Map<String, Object> params){
        if (params.get("catelogId").toString() == "0" && params.get("brandId").toString() == "0" &&params.get("min").toString() == "0" &&params.get("max").toString() == "0" ){
            PageUtils pageUtils = skuInfoService.queryPage(params);
            return R.ok().put("page",pageUtils);
        }else {
            PageUtils pageUtils = skuInfoService.querySkuPage(params);
            return R.ok().put("page",pageUtils);
        }

    }


    /**
     * 删除
     */
    @RequestMapping("commodity/skuinfo/delete")
    public R delete(@RequestBody Long[] skuIds){
		skuInfoService.removeByIds(Arrays.asList(skuIds));

        return R.ok();
    }

    /**
     * 根据skuId查询当前商品的价格
     * @param skuId
     * @return
     */
    @GetMapping(value = "/product/skuinfo/{skuId}/price")
    public BigDecimal getPrice(@PathVariable("skuId") Long skuId) {

        //获取当前商品的信息
        SkuInfoEntity skuInfo = skuInfoService.getById(skuId);

        //获取商品的价格
        BigDecimal price = skuInfo.getPrice();

        return price;
    }
}
